<template>
  <div class="login">
    <div class="login-con">
      <div class="login_title">
        <img src="@/assets/login/loginlogo.png" alt />
      </div>

      <div class="center-box">
        <div class="userLogin-title">用户登录</div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号">
              <img style="width: 22px; height: 25px" slot="prefix" src="@/assets/login/user.png" alt class="img_slot" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
              @keyup.enter.native="handleLogin">
              <img style="width: 1.3vw; height: 2.6vh" slot="prefix" src="@/assets/login/password.png" alt
                class="img_slot" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" class="code-par">
            <el-input v-model="loginForm.code" auto-complete="off" placeholder="请输入验证码"
              @keyup.enter.native="handleLogin">
              <img style="width: 22px; height: 25px" slot="prefix" src="@/assets/login/code.png" alt class="img_slot" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" />
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin: 5px 0 !important;">记住密码</el-checkbox>
          <el-form-item style="width: 100%">
            <el-button :loading="loading" size="medium" type="primary"
              style="width: 100%; border-radius: 10px; height: 7.8vh" @click.native.prevent="handleLogin">
              <span v-if="!loading" style="font-size: 28px">登 录</span>
              <span v-else style="font-size: 22px">登 录 中...</span>
            </el-button>
            <div style="float: right" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2025 动车入库作业智能登销记系统</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, getmenu } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
        password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set("rememberMe", this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect }).catch(() => { });
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>


<style lang="scss" scoped>
$btn-blue: #00a0e9;

.login {
  --input-height: 65px;
  --form-item-gap: 1vh;
  --title-font-size: 28px;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: url("../assets/login/bg.jpg") no-repeat center;
  background-size: 100% 100%;
  position: relative;
}

.login-con {
  width: 30vw;
  height: 62vh;
  min-width: 350px;
  min-height: 500px;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

.login_title {
  top: 12.037%;
  left: 7.8125%;
  position: fixed;
  width: 661px;
  height: 40px;

  img {
    width: 661px;
    height: 40px;
  }
}

.userLogin-title {
  color: $btn-blue;
  font-size: clamp(24px, 1.8vw, 32px);
  padding: 2.5vh 0;
  text-align: center;
  margin-bottom: 2vh;
}

.center-box {
  width: 31.7vw;
  height: auto;
  min-height: 50vh;
  max-height: 85vh;
  border-radius: 10px;
  background: #f7f7f7;
  position: fixed;
  top: 22.389%;
  right: 8.333%;
  box-shadow: 8px 7px 5px rgba(#0a2f47, 0.25);
  overflow-y: auto;
  padding: 2vh 1.5vw;
  box-sizing: border-box;
}

.login-form {
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
  padding: 0 5% 3vh 5%;

  .img_slot {
    vertical-align: middle;
    width: 22px !important;
    height: 25px !important;
  }

  .el-form-item {
    margin-bottom: var(--form-item-gap) !important;
    
    &:last-of-type {
      margin-bottom: 1vh !important;
      margin-top: 3vh;
    }
  }

  .el-form-item__content {
    height: auto;
    line-height: normal;
  }

  // 关键修复：使用 ::v-deep 穿透 scoped，并定位真实输入框
  ::v-deep .el-input {
    height: auto;

    .el-input__prefix {
      width: 60px;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      padding-right: 10px;
    }

    // 核心：使用 .el-input__inner 而不是 input
    .el-input__inner {
      border-radius: 0;
      background: #eeeeee;
      height: var(--input-height) !important;
      min-height: 50px !important;
      max-height: 70px !important;
      line-height: normal !important;
      font-size: clamp(16px, 1.2vw, 20px);
      padding-left: 75px;
      width: 100%;
      color: #bbbbbc;
      box-sizing: border-box;
    }
  }
}

.code-par {
  position: relative;
}

.login-code {
  position: absolute;
  right: 10px;
  width: 90px;
  height: 35px;
  top: 50%;
  transform: translateY(-50%);

  img {
    cursor: pointer;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.el-checkbox {
  width: 100%;
  text-align: right;
}

.el-button--primary {
  color: #fff;
  background-color: #297ac7;
  border-color: #297ac7;
  width: 100%;
  border-radius: 10px;
  height: auto;
  min-height: 60px;
  padding: 1.5vh 2vw;
  font-size: clamp(18px, 1.5vw, 28px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

// Element UI 覆盖 - 深度选择器
::v-deep .el-checkbox__input .el-checkbox__inner {
  width: 15px;
  height: 15px;
}

::v-deep .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: $btn-blue;
  border-color: $btn-blue;
}

::v-deep .el-checkbox__input.is-checked + .el-checkbox__label {
  color: $btn-blue;
}

// ==================== 媒体查询 - 核心修正 ====================
@media (max-width: 1920px) {
  .login {
    --input-height: 62px;
    --form-item-gap: 2.2vh;
  }
}

@media (max-width: 1440px) {
  .login {
    --input-height: 58px;
    --form-item-gap: 2vh;
  }
}

@media (max-width: 1366px) {
  .login {
    --input-height: 55px;
    --form-item-gap: 1.8vh;
  }
  
  .center-box {
    width: 35vw;
  }
}

@media (max-width: 1024px) {
  .login {
    --input-height: 52px;
    --form-item-gap: 2vh;
    --title-font-size: 26px;
  }
  
  .login-con {
    width: 65vw;
    max-width: 450px;
  }
  
  .center-box {
    width: 100%;
    right: auto;
    left: 50%;
    transform: translateX(-50%);
    padding: 2vh 4vw;
  }
}

@media (max-width: 768px) {
  .login {
    --input-height: 50px;
    --form-item-gap: 3.5vh;
    --title-font-size: 24px;
  }
  
  .login-con {
    width: 85vw;
    max-width: 380px;
    min-height: 450px;
  }
  
  .center-box {
    padding: 1.5vh 5vw;
    max-height: 90vh;
  }
  
  // 移动端特殊处理
  .login-form ::v-deep .el-input__inner {
    font-size: 16px;
    padding-left: 65px;
  }
  
  ::v-deep .el-input__prefix {
    width: 50px !important;
  }
  
  .login-code {
    width: 80px;
    height: 30px;
  }
  
  .el-button--primary {
    min-height: 55px;
    font-size: 20px;
  }
}

@media (max-width: 480px) {
  .login {
    --input-height: 48px;
    --form-item-gap: 3vh;
    --title-font-size: 22px;
  }
  
  .login-con {
    width: 92vw;
    max-width: 320px;
    min-height: 400px;
  }
  
  .center-box {
    padding: 1vh 4vw;
  }
  
  .login-form ::v-deep .el-input__inner {
    padding-left: 60px;
  }
  
  ::v-deep .el-input__prefix {
    width: 45px !important;
  }
  
  .login-code {
    width: 70px;
    height: 28px;
    right: 8px;
  }
  
  .el-button--primary {
    min-height: 50px;
    font-size: 18px;
  }
}

@media (max-width: 375px) {
  .login {
    --input-height: 45px;
    --form-item-gap: 2.8vh;
  }
  
  .login-con {
    max-width: 300px;
  }
  
  .userLogin-title {
    font-size: 20px;
    padding: 2vh 0;
  }
  
  .login-form ::v-deep .el-input__inner {
    padding-left: 55px;
  }
}
</style>

